package com.xyc.view.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xyc.global.bean.PageBean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

@Controller
@RequestMapping("page")
public class PageController {

    private Logger logger = Logger.getLogger("PageController");
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //登录
    @RequestMapping("login.html")
    public String toLogin(){
        return "login";
    }

    //商户首页
    @RequestMapping("merchant-index.html")
    public String toIndex(){
        return "merchant/index";
    }

    @RequestMapping("goods-add.html")
    public String toPage(Model model){
        Random random = new Random();
        int nextInt = random.nextInt(100000);
        model.addAttribute("goodsId","qf"+nextInt);
        return "merchant/goods-add";
    }

    //商户信息
    @RequestMapping("goods-info.html")
    public String toGoodsInfo(Integer currentPage, Model model, Gson gson){
        if (currentPage == null){
            currentPage = 1;
        }
        Type type = new TypeToken<PageBean>() {
        }.getType();

        String key ="sys-goods-list-"+currentPage;
        String goodsJsonCache = stringRedisTemplate.boundValueOps(key).get();
        PageBean pageBean = gson.fromJson(goodsJsonCache,type);
        if (goodsJsonCache == null){
            pageBean = restTemplate.getForObject("http://merchant-server/goodsOO/pageBean?currentPage="+currentPage,PageBean.class);

            String json = gson.toJson(pageBean, type);
            stringRedisTemplate.boundValueOps(key).set(json,30, TimeUnit.MINUTES);
        }
        logger.info("toGoodsInfo-->currentPage"+currentPage);
        model.addAttribute("pageBean",pageBean);
        return "merchant/goods-info";
    }

    //商户文案
    @RequestMapping("goods-copy.html")
    public String toGoodsCopy(){
        return "merchant/goods-copy";
    }

    //自媒体
    @RequestMapping("wemedia-index.html")
    public String tojsIndex(){
        return "wemedia/index";
    }

    @RequestMapping("goods-and-ad.html")
    public String toGoodsAndAdPage(){
        return "wemedia/goods-and-ad";
    }

    @RequestMapping("goods-ad-details.html")
    public String toGoodsAdDetailsPage(){
        return "wemedia/goods-ad-details";
    }

    @RequestMapping("order-details.html")
    public String toOrderDetails(){
        return "wemedia/order-details";
    }

    @RequestMapping("my-recomed-order.html")
    public String toMyRecomedOrder(){
        return "wemedia/my-recomed-order";
    }
}
